package com.edu.ad.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.api.entity.ad.PromotionSpace;
import com.edu.ad.service.PromotionSpaceService;
import com.edu.ad.mapper.PromotionSpaceMapper;
import com.edu.api.entity.common.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class PromotionSpaceServiceImpl extends ServiceImpl<PromotionSpaceMapper, PromotionSpace>
    implements PromotionSpaceService{

    @Autowired
    PromotionSpaceMapper spaceMapper;

    @Override
    public ResponseDTO addOrUpdateBackStageSpace(PromotionSpace space, HttpServletRequest request) {
        try {
            QueryWrapper<PromotionSpace> queryWrapper = new QueryWrapper();
            if (space.getId()==null){
                queryWrapper.eq("space_key",space.getSpaceKey()).or().eq("name",space.getName()).eq("is_del",0);
                if (!spaceMapper.selectList(queryWrapper).isEmpty()){
                    return new ResponseDTO(Code.SAVE_ERR,"广告位或key重复",null);
                }
                return new ResponseDTO(Code.SAVE_OK,"添加成功",null);
            }else {
                queryWrapper.eq("is_del",0).ne("id",space.getId());
                queryWrapper.and(wrapper -> wrapper.eq("space_key",space.getSpaceKey()).or().eq("name",space.getName()));
                if (!spaceMapper.selectList(queryWrapper).isEmpty()){
                    return new ResponseDTO(Code.SAVE_ERR,"广告位或key重复",null);
                }
                spaceMapper.updateById(space);
                return new ResponseDTO(Code.SAVE_OK,"修改成功",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseDTO(Code.SAVE_OK,"操作失败",null);
        }
    }

    @Override
    public ResponseDTO getAllSpace(Pageable pageable, PromotionSpace space) throws ParseException {
        QueryWrapper<PromotionSpace> queryWrapper = new QueryWrapper<>();
        Date parse = null;
        Date parse2 = null;
        if (!space.getCreateTime().equals("NaN-NaN-NaN")){
            parse = new SimpleDateFormat("yyyy-MM-dd").parse(space.getCreateTime());
            parse2 = new SimpleDateFormat("yyyy-MM-dd").parse(space.getUpdateTime());
        }
        queryWrapper.eq("is_del",0).like("name",space.getName()).like("space_key",space.getSpaceKey())
                .ge(!"NaN-NaN-NaN".equals(space.getCreateTime()),"create_time",parse)
                .le(!"NaN-NaN-NaN".equals(space.getUpdateTime()),"update_time",parse2);
        long total = spaceMapper.selectCount(queryWrapper);
        long pages = 0;
        if(total>0){
            //计算总页码
            pages = total % pageable.getSize() == 0 ? total / pageable.getSize() : total / pageable.getSize() + 1;
            //页码修正
            pageable.setPage(pageable.getPage() < 1 ? 1 : pageable.getPage());
            pageable.setPage(pageable.getPage() > pages ? pages : pageable.getPage());
            //起始下标
            pageable.setIndex((pageable.getPage() - 1) * pageable.getSize());
        }else{
            pageable.setPage(0);
            pageable.setIndex(0);
        }
        Page<PromotionSpace> spacePage = new Page(spaceMapper.getAllSpace(pageable,space), pageable.getPage(), total, pages);

        return new ResponseDTO(Code.GET_OK,"成功",spacePage);
    }

    @Override
    public ResponseDTO deleteSpaceByIds(List ids) {
        spaceMapper.deleteSpaceByIds(ids);
        return new ResponseDTO(Code.DELETE_OK,"成功",null);
    }
}




